/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ocr.handler;

import cn.ac.iie.di.ocr.util.FtpTools;
import cn.ac.iie.di.ocr.util.GlobalParameters;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;

/**
 *
 * @author Administrator
 */
public class OCRThreadPoolTask implements Runnable {

    static private final Logger logger = Logger.getLogger(OCRThreadPoolTask.class.getName());
    private Message list;
    private FTPClient createFtpClient = null;
    private FTPClient deleteFtpClient = null;
    private String ftpip = "";
    private String ftppath = "";
    private Integer ftpport = -1;
    private String username = "";
    private String passwd = "";

    public OCRThreadPoolTask(Message list) {
        this.list = list;
    }

    @Override
    public void run() {
        try {
            logger.debug("get " + list.getData().size() + " datas form picHandlerQueue!");
            List names = Arrays.asList(list.getColNames());
            int userid = names.indexOf("userid");
            int ct = names.indexOf("ct");
            int groupid = names.indexOf("GROUPgroupidID");
            Integer startTime = null;
            Integer lasttime = null;
            String[] colNames = list.getColNames();
//            logger.info("list.getColNames().size: " + colNames.length);
            for (int i = 0; i < colNames.length; i++) {
                if ("userid".equals(colNames[i])) {
                    userid = i;
                }
                if ("ct".equals(colNames[i])) {
                    ct = i;
                }
                if ("groupid".equals(colNames[i])) {
                    groupid = i;
                }
                if (colNames.length > 3) {
                    if ("starttime".equals(colNames[i])) {
                        startTime = i;
                    }
                }
                if (colNames.length > 4) {
                    if ("lasttime".equals(colNames[i])) {
                        lasttime = i;
                    }
                }
            }
//            logger.info("ppppppppppppp " + userid + "_" + ct + "_" + groupid);
//            logger.info("colNames " + names.size());
            for (Object[] msg : list.getData()) {
                Map m = new HashMap();
                m.put("userid", msg[userid].toString());
                m.put("ct", msg[ct].toString());
                m.put("groupid", msg[groupid].toString());
                if (startTime != null) {
                    m.put("starttime", msg[startTime].toString());
                    if (null != lasttime) {
                        m.put("lasttime", msg[lasttime].toString());
                    }
                } else {
                    m.put("starttime", System.currentTimeMillis() / 1000);
                }
                Long time = Long.valueOf(m.get("starttime").toString());
                if ((System.currentTimeMillis() / 1000 - time) > 172800) {
                    logger.info("the opid select all mpp is null userid: " + m.get("userid") + " ct: " + m.get("ct") + " groupid: " + m.get("groupid"));
                    continue;
                }
                if (m.containsKey("lasttime")) {
                    if (System.currentTimeMillis() - Long.valueOf(m.get("lasttime").toString()) > 900000) {
                        logger.debug("lasttime is : " + Long.valueOf(m.get("lasttime").toString()) + "_" + (System.currentTimeMillis() - Long.valueOf(m.get("lasttime").toString())));
                        GlobalParameters.sendRetryQueue.put(m);
                        continue;
                    }
                } else {
                    m.put("lasttime", System.currentTimeMillis());
                }
                if (GlobalParameters.flag) {
                    GlobalParameters.sendRetryQueue.put(m);
                } else {
                    GlobalParameters.mppQueue.put(m);
                }

                logger.debug("mppQueue " + m.toString() + " size " + GlobalParameters.mppQueue.size());
            }

        } catch (Exception e) {
            logger.error("Set redis error :" + e + list.toString(), e);
        } finally {
//            if (jedis != null) {
//                GlobalParameters.connectionHandler.returnResource(jedis);
//            }
        }

    }

}
